/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.enterprisebeans;

import ca.trader.logger.TraderLogger;
import ca.trader.sale.Sale;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

/** 
 * Stateless bean for performing actions on Sale objects
 * 
 * @author Ahsan
 */
@Stateless
public class SaleBean implements SaleBeanLocal
{
	 private static final String _className = SaleBean.class.getName();
	 
	 /**
	  * Entity manager for persisting or retrieving sale objects
	  */
	 @PersistenceContext(unitName = TraderPersistenceConsts.PERSISTENCE_UNIT_NAME, type = PersistenceContextType.TRANSACTION)
	 private EntityManager entityManager;
	 
 	 	 
	 /**
	  * inserts a sale entity bean into the database.
	  * Throws IllegalArgumentException if the bean is null
	  * @param sale the sale entity bean to insert
	  * @exception IllegalArgumentException if the sale entity bean is null
	  */
	 @Override
	 @TransactionAttribute(TransactionAttributeType.REQUIRED)
	 public void insertSale(Sale sale)
	 {
		  String _methodName = "insertSale";

		  TraderLogger.logMethodEntry(_className, _methodName);

		  TraderLogger.logInfo(_className, _methodName, "Attempting sale insert.");

		  // if the sale entity object is null, throw IllegalArgumentException
		  if (sale == null)
		  {
			   TraderLogger.logWarning(_className, _methodName, "Sale is null. Exiting");
			   TraderLogger.logMethodExit(_className, _methodName);
			   throw new IllegalArgumentException("Sale is null");
		  }

		  // persist the sale
		  entityManager.persist(sale);

		  TraderLogger.logInfo(_className, _methodName, "Successfully inserted new sale into the sale list");

		  TraderLogger.logMethodExit(_className, _methodName);
	 }


}
